import moment from 'moment'
import store from '../vuex/store'
import messages from '../i18n/langs/index'
import '../fonts/iconfont'

export default {
  bubble: function(name,motion,speed,course,accuracy,mode,time,state) {
     return '<ul><li><b>'+name+'</b><span class=\"bubble-mode\">'+
     (state.rssi?" &nbsp;<svg title=\'GSM信号\' class=\"icon\" aria-hidden=\"true\"><use xlink:href=\"#icon-gongyong-xinhao"+state.rssi+"\"></use></svg>":"") +
     (mode == 0 ? " <i title=\'GPS定位\' class=\"iconfont icon-height-light-6\"></i>" + (!state.sat? "" : " <span style=\"font-size : 0.3em;\">" + state.sat + "</span>") : mode == 1 ? " <i title=\'基站\' class=\"iconfont icon-jizhan\"></i>" + (!accuracy? "" : "<span style=\"font-size : 0.3em;\">" + accuracy + "m</span>") : mode == 2 ? " <i title=\'wifi\' class=\"iconfont icon-wifi\"></i>" + (!accuracy? "" : "<span style=\"font-size : 0.3em;\">" + accuracy + "m</span>") : "") +
     '</span></li>'+
     '<li>状态:'+(motion==1?parseInt(speed)+'km/h ('+this.formartCourse(course)+')':'静止('+this.beforeTime(time)+')')+(state.alarm?'[<font color=red>'+store.state.alarms[state.alarm]+'</font>]':'')+" </li>"+
     '<li>里程:'+(state.todaytrip?state.todaytrip:0)+'/'+(state.trip?state.trip:0)+'km</li>'+
     '<li>时间:'+this.dateFormat(time,'YYYY-MM-DD HH:mm:ss')+'</li>'
     +this.state(state)+'</ul>'
  },
  state: function(state){
     if(state){
       return '<li>'+(state.battery?"<div title='电量"+state.battery+"%' data-charge=\"" + (Math.round(state.battery / 100 * 6)) + "\" class=\"battery\"></div> ":'')+
       (!state.voltage?'':'<i title=\'电压\' class=\"fa fa-plug\"></i>'+state.voltage+'v')+
       (!state.ignition?'':''+(state.ignition?'<span title=\'ACC:'+state.ignition+'\'><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fadongji"></use></svg></span>':'<span title=\'ACC:'+state.ignition+'\'><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fadongji"></use></svg></span>'))+
       (!state.defense?'':''+(state.defense?'<span title=\'设防\'><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-lockclose"></use></svg></span>':'<span title=\'撤防\'><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-lockopen"></use></svg></span>'))+
       (!state.temp?'':'<span title=\'温度\'><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-temp"></use></svg>'+state.temp+'℃</span>')+
       (!state.turn?'':'<span title=\'正反转\'><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-jiaobanche"></use></svg>'+store.state.deviceStates[state.turn]+'</span>')+
       (!state.fuelPercentage?'':'<span title=\'油量\'><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fuel"></use></svg>'+state.fuelPercentage+'%</span>')+
       (!state.image?'':'<span title=\'照片\' class="show-image"><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-photo"></use></svg><div id="img-popup"><img src="http://oss.taiyangos.com/device-media/014143372469/'+state.image+'"/></div></span>')+
       '</li>'
     }else{
      return ''
    }
  },

  formartCourse: function(course){
      if(course<=5||course>355){
           return messages[store.state.global.lang].map.north;
       }else if(course>5&&course<=85){
         return messages[store.state.global.lang].map.northEast;
       }else if(course>85&&course<=95){
         return messages[store.state.global.lang].map.east;
       }else if(course>95&&course<=175){
         return messages[store.state.global.lang].map.southEast;
       }else if(course>175&&course<=185){
         return messages[store.state.global.lang].map.south;
       }else if(course>185&&course<=265){
         return messages[store.state.global.lang].map.southWest;
       }else if(course>265&&course<=275){
         return messages[store.state.global.lang].map.west;
       }else if(course>275&&course<=355){;
         return messages[store.state.global.lang].map.northWest;
       }else{
         return '未知';
       }
  },
   //时间格式化
    dateFormat:function(t,f) {
              if (t == undefined) {
                 return "";
              }
              return moment(t).format(f);
    },
    //获取前多少天的日期
    getBefareDay:function(d){
        return moment().subtract(d, 'days').hour(0).minute(0).second(0);
    },
    motion:function(time){
      var now = moment()
      var then = moment(time)
      if(now.diff(then, 'minute')<=5){
         return 1
      }else{
         return 0
      }
    },
    beforeTime:function(val){
    let nowDate = new Date();
    let replyDate = new Date(val);
    let diffSeconds = (nowDate.getTime()-Number(replyDate.getTime()))/1000;
    let years = 365*24*60*60;
    let months = 30*24*60*60;
    let days = 24*60*60;
    let hours =  60*60;
    let minutes = 60;
    let seconds = 1;
    if(diffSeconds<seconds){
        return '1秒以前'
    }else if(seconds<=diffSeconds&&diffSeconds<minutes){
        return Math.floor(diffSeconds/seconds)+messages[store.state.global.lang].map.second
    }else if(minutes<=diffSeconds&&diffSeconds<hours){
        return Math.floor(diffSeconds/minutes)+messages[store.state.global.lang].map.minute
    }else if(hours<diffSeconds&&diffSeconds<days){
        return Math.floor(diffSeconds/hours)+messages[store.state.global.lang].map.hour
    }else if(days<diffSeconds&&diffSeconds<months){
        return Math.floor(diffSeconds/days)+messages[store.state.global.lang].map.day
    }else if(months<diffSeconds&&diffSeconds<years){
        return Math.floor(diffSeconds/months)+messages[store.state.global.lang].map.month
    }else{
        return Math.floor(diffSeconds/years)+messages[store.state.global.lang].map.year
    }
},
stopTime:function(diff){
let years = 365*24*60*60;
let months = 30*24*60*60;
let days = 24*60*60;
let hours =  60*60;
let minutes = 60;
if(minutes<=diff&&diff<hours){
    return Math.floor(diff/minutes)+'分'
}else if(hours<diff&&diff<days){
    return Math.floor(diff/hours)+'小时'
}else if(days<diff&&diff<months){
    return Math.floor(diff/days)+'天'
}else if(months<diff&&diff<years){
    return Math.floor(diff/months)+'月'
}else{
    return Math.floor(diff/years)+'年'
}
},
fullScreen: function() { //全屏显示
		var docElm = document.documentElement;
		//W3C
		if(docElm.requestFullscreen) {
			docElm.requestFullscreen();
		}
		//FireFox
		else if(docElm.mozRequestFullScreen) {
			docElm.mozRequestFullScreen();
		}
		//Chrome等
		else if(docElm.webkitRequestFullScreen) {
			docElm.webkitRequestFullScreen();
		}
		//IE11
		else if(elem.msRequestFullscreen) {
			elem.msRequestFullscreen();
		} else {
			("您的浏览器版本过低,暂不支持此操作</br>请升级浏览器后再操作").showTips();
		}
	},
	exitFullScreen: function() { //退出全屏
		if(document.exitFullscreen) {
			document.exitFullscreen();
		} else if(document.mozCancelFullScreen) {
			document.mozCancelFullScreen();
		} else if(document.webkitCancelFullScreen) {
			document.webkitCancelFullScreen();
		} else if(document.msExitFullscreen) {
			document.msExitFullscreen();
		} else {
			("您的浏览器版本过低,暂不支持此操作</br>请升级浏览器后再操作").showTips();
		}
	},

/**
   * 将list装换成tree
   * @param {Object} myId 数据主键id
   * @param {Object} pId  数据关联的父级id
   * @param {Object} list list集合
   */
  listToTree:function(myId,pId,list){
   function exists(list, parentId){
    for(var i=0; i<list.length; i++){
     if (list[i][myId] == parentId) return true;
    }
    return false;
   }

   var nodes = [];
   // get the top level nodes
   for(var i=0; i<list.length; i++){
    var row = list[i];
    if (!exists(list, row[pId])){
      if(row.children){
        delete row.children;
      }
      nodes.push(row);
    }
   }

   var toDo = [];
   for(var i=0; i<nodes.length; i++){
    toDo.push(nodes[i]);
   }
   while(toDo.length){
    var node = toDo.shift(); // the parent node
    // get the children nodes
    for(var i=0; i<list.length; i++){
     var row = list[i];
     if (row[pId] == node[myId]){
      //var child = {id:row.id,text:row.name};
      if (node.children){
       node.children.push(row);
      } else {
       node.children = [row];
      }
      toDo.push(row);
     }
    }
   }
   return nodes;
 },




  //按日查询
   getDayAll:function(start_time,end_time){
     var bd = new Date(start_time),be = new Date(end_time);
      var bd_time = bd.getTime(), be_time = be.getTime(),time_diff = be_time - bd_time;
      var d_arr = [];
      for(var i=0; i<= time_diff; i+=86400000){
              var ds = new Date(bd_time+i);
              d_arr.push(this.dateFormat(ds,'YYYY-MM-DD'))
      }
      return d_arr;
   },

 ////////////////////////////////////////////////////////////////////////

};
